﻿using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace Gamemaster.RollTests
{
    public class d100Tests
    {
        [Fact]
        public void AverageIsCorrect()
        {
            List<int> results = RollManyTimes();

            double mean = results.Average();
            Assert.InRange(mean, 45, 55);   // Increasing tolerance
        }

        [Fact]
        public void RangeIsCorrect()
        {
            List<int> results = RollManyTimes();            

            bool outOfRange = results.Any(r => r < 1 || r > 100);
            Assert.False(outOfRange);
        }

        [Fact]
        public void ProducesAtLeastHalfThePossibleNumbers()
        {
            List<int> actual = RollManyTimes();
            List<int> expected = new List<int>();

            for (int i = 1; i <= 100; i++)
            {
                expected.Add(i);
            }

            IEnumerable<int> notFound = expected.Except(actual);

            // Assume that the chances of not getting a few of the
            // number is the range is not improbable, but at least
            // half of them should be present.
            Assert.InRange(notFound.Count(), 0, 50);
        }

        List<int> RollManyTimes()
        {
            List<int> results = new List<int>();

            for (int i = 0; i < 3000; i++)
            {
                results.Add(Roll.d100());
            }

            return results;
        }
    }
}
